package com.company.LambdaDemo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;

public class StreamMain {
    public static void main(String[] args) {
        List<ModelA> list = initData(10000);

        System.out.println(list.stream().sorted(Comparator.comparing(ModelA::getAge)).map(ModelA::getAge)
            .collect(Collectors.toList()));

        Long now = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            // testNormalSet(list);
            testStreamSet(list);
            // testNormal(list);
            // testStream(list);
        }

        List<String> stringList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            stringList.add(String.valueOf(i));
        }
        System.out.println(stringList.stream().collect(Collectors.joining(",")));
        System.out.println(System.currentTimeMillis() - now);
    }

    static List<ModelA> initData(int total) {
        List<ModelA> list = new ArrayList<>(total);
        for (int i = 0; i < total; i++) {
            ModelA model = new ModelA();
            model.setAge(i);
            model.setDate(DateUtil.date());
            model.setName("测试" + i);
            list.add(model);
        }
        return list;
    }

    static List<ModelB> testNormalSet(List<ModelA> list) {
        List<ModelB> bList = new ArrayList<>(list.size());
        for (ModelA item : list) {
            ModelB model = new ModelB();
            model.setAge(item.getAge());
            model.setDate(item.getDate());
            model.setName(item.getName());
            bList.add(model);
        }
        return bList;
    }

    static List<ModelB> testStreamSet(List<ModelA> list) {
        return list.stream().map(item -> {
            ModelB model = new ModelB();
            model.setAge(item.getAge());
            model.setDate(item.getDate());
            model.setName(item.getName());
            return model;
        }).collect(Collectors.toList());
    }

    static List<ModelB> testNormal(List<ModelA> list) {
        List<ModelB> bList = new ArrayList<>(list.size());
        for (ModelA item : list) {
            ModelB model = new ModelB();
            BeanUtil.copyProperties(item, model);
            bList.add(model);
        }
        return bList;
    }

    static List<ModelB> testStream(List<ModelA> list) {
        return list.stream().map(item -> {
            ModelB model = new ModelB();
            BeanUtil.copyProperties(item, model);
            return model;
        }).collect(Collectors.toList());
    }
}

class ModelA {
    private Integer age;
    private String name;
    private Date date;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

class ModelB {
    private Integer age;
    private String name;
    private Date date;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}
